package com.payment.notify.service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.payment.common.exceptions.ServiceException;
import com.payment.notify.core.NotifyQueue;
import com.payment.notify.domain.TblTradeNotifyRecord;
import com.payment.notify.domain.TblTradeNotifyRecordLog;
import com.payment.notify.repository.TblTradeNotifyRecordLogMapper;
import com.payment.notify.repository.TblTradeNotifyRecordMapper;

/**
 * 类名：NotifySerivce.java<br>
 * 描述：<br>
 * 创建者：XINJF<br>
 * 创建日期：2018年4月21日 上午11:58:00<br>
 * 版本：1.0<br>
 * 修改者：<br>
 * 修改日期：<br>
 */
@Service("notifySerivce")
@Transactional(rollbackFor = ServiceException.class)
public class NotifySerivce {
	
	private Logger logger = LoggerFactory.getLogger(NotifySerivce.class);
	
	@Autowired
	private TblTradeNotifyRecordMapper tblTradeNotifyRecordMapper;
	
	@Autowired
	private TblTradeNotifyRecordLogMapper tblTradeNotifyRecordLogMapper;
	
	
	
	/**
	 * 功能描述:<br>
	 * 创建者：XINJF<br>
	 * 创建日期:<br>
	 * 版本：1.0<br>
	 * 修改者：<br>
	 * 修改日期：<br>
	 */
	public TblTradeNotifyRecord getNotifyByBusiidNotifyType(Integer busiId,Integer notifyType) throws ServiceException{
		return tblTradeNotifyRecordMapper.selectByPrimaryKey(1L);
		
	} 

	/**
	 * 功能描述:获取消息通知<br>
	 * 创建者：XINJF<br>
	 * 创建日期:<br>
	 * 版本：1.0<br>
	 * 修改者：<br>
	 * 修改日期：<br>
	 */
	public TblTradeNotifyRecord getNotifyById(Long notifyId) throws ServiceException {
		return tblTradeNotifyRecordMapper.selectByPrimaryKey(notifyId);
	}


	/**
	 * 
	 * 功能描述:创建通知记录<br>
	 * 创建者：XINJF<br>
	 * 创建日期:<br>
	 * 版本：1.0<br>
	 * 修改者：<br>
	 * 修改日期：<br>
	 */
	public Long creatNotifyRecord(TblTradeNotifyRecord notifyRecord) throws ServiceException {
		Integer flag = tblTradeNotifyRecordMapper.insertSelective(notifyRecord);  
		return flag == 1 ? notifyRecord.getId() : flag; 
	}

	/**
	 * 
	 * 功能描述:更新通知<br>
	 * 创建者：XINJF<br>
	 * 创建日期:<br>
	 * 版本：1.0<br>
	 * 修改者：<br>
	 * 修改日期：<br>
	 */
	public Integer updateNotifyRecord(TblTradeNotifyRecord notifyRecord) throws ServiceException {
		return tblTradeNotifyRecordMapper.updateByPrimaryKeySelective(notifyRecord);
	}

	/**
	 * 功能描述:创建通知日志记录<br>
	 * 创建者：XINJF<br>
	 * 创建日期:<br>
	 * 版本：1.0<br>
	 * 修改者：<br>
	 * 修改日期：<br>
	 */
	public Integer creatNotifyRecordLog(TblTradeNotifyRecordLog notifyRecordLog) throws ServiceException {
		return this.tblTradeNotifyRecordLogMapper.insertSelective(notifyRecordLog);
	}
	
	public List<TblTradeNotifyRecord> selectAll(){
		return this.tblTradeNotifyRecordMapper.selectAll();
	}
	
	public List<Object> selectAllBYCondition(Map<String, Object> map){
		return this.tblTradeNotifyRecordMapper.selectAllBYCondition(map);
	}
	
	public void startInitFromDB(NotifyQueue notifyQueue) {
		//TODO 读取未发送的数据
		int pageNum = 1;
	    int pageSize = 500;
        // 查询状态和通知次数符合以下条件的数据进行通知
        String[] status = new String[] { "101", "102", "200", "201" };
        Integer[] notifyTime = new Integer[] { 0, 1, 2, 3, 4 };
        // 组装查询条件
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put("statusList", status);
        paramMap.put("notifyTimeList", notifyTime);
        PageHelper.startPage(pageNum, pageSize);
        
        List<Object> pager = this.selectAllBYCondition(paramMap);
        PageInfo pageInfo = new PageInfo(pager);
        long pages = pageInfo.getPages();//总页数
        while (pageNum <= pages) {
        	for (Object object : pageInfo.getList()) {
        		TblTradeNotifyRecord notifyRecord = (TblTradeNotifyRecord) object;
        		notifyRecord.setLastNotifyTime(new Date());
        		notifyQueue.addElementToList(notifyRecord);
			}
            pageNum++;
            logger.info(String.format("调用通知服务.notifyService.selectAllBYCondition(%s, %s, %s)", pageNum, pageSize, paramMap));
            PageHelper.startPage(pageNum, pageSize);
            pager = this.selectAllBYCondition(paramMap);
            pageInfo = new PageInfo(pager);
        }
	}
	
		

}
